//
//  LYCMarketMenuView.m
//  爱鲜蜂
//
//  Created by 廖易诚 on 16/9/6.
//  Copyright © 2016年 itcast. All rights reserved.
//

#import "LYCMarketMenuView.h"
#import "MarketMenuModel.h"

@interface LYCMarketMenuView ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) NSIndexPath *indexPath;
@end

@implementation LYCMarketMenuView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        self.dataSource = self;
        self.delegate = self;
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(menuScrollToPosition:) name:@"menuScrollToPosition" object:nil];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
            UITableViewCell *cell = [self cellForRowAtIndexPath:indexPath];
            cell.backgroundColor = [UIColor whiteColor];
        });
    }
    return self;
}

#pragma mark - 滚动到指定位置
- (void)menuScrollToPosition:(NSNotification *)noti {
    NSNumber *section = noti.object;
//    
//    UITableViewCell *lastCell = [self cellForRowAtIndexPath:self.indexPath];
//    lastCell.backgroundColor = [UIColor lightGrayColor];
//    
//    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[section integerValue] inSection:0];
//    self.indexPath = indexPath;
//    UITableViewCell *cell = [self cellForRowAtIndexPath:indexPath];
//    cell.backgroundColor = [UIColor whiteColor];
//    
}

/// 设置多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.menus.count;
}

// 显示每一个cell的样式
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellID = @"cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    
    if ( !cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    MarketMenuModel *model = self.menus[indexPath.row];
    cell.backgroundColor = [UIColor lightGrayColor];
    cell.textLabel.text = model.name;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)setMenus:(NSArray *)menus {
    _menus = menus;
    [self reloadData];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    self.indexPath = indexPath;
    
    
    MarketMenuModel *model = self.menus[indexPath.row];
    self.changeRightViewDataSourceBlock(model);
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:self.indexPath];
    cell.backgroundColor = [UIColor lightGrayColor];
}




@end
